var reqwest = require('reqwest');
window.common = {
  ajax: ajax,
  mpaginate: mpaginate,
}

/**
 * 网络请求
 * param* url {string}
 * param* params {object}
 * param* success {func}
 * param error {func}
 * param method {string}
 * param contentType {string}
 */
function ajax(option) {
  var url = option.url;
  var params = option.params || {};
  var success = option.success;
  var error = option.error || null;
  var method = option.method || 'post';
  var contentType = option.contentType || "application/json";
  // common.loading('addLoading');
  reqwest({
    url: url,
    method: method,
    data: params,
    contentType: contentType,
    type: 'json',
    success: function(data) {
      success(data);
    },
    error: function(err) {
      console.log(err);
    }
  })
}


/**
 *  通过mescroll插件进行上拉刷新和下拉加载更多的统一封装处理
 *  @param {string} url* 请求的url
 *  @param {func} callback* 请求成功后的回调
 *  @param {number} pageNo  请求的url
 *  @param {number} pageSize 请求的url
 *  mescroll.endErr();
 *  pageNo
 *  pageSize
 *  list
 */
function mpaginate(options) {
  var url = options.url;
  var callback = options.callback || null;
  var pageNo = options.pageNo || 1;
  var pageSize = options.pageSize || 3;
  var infiniteLoading = false;  //上拉加载更多的状态
  var returnTotalLists = [];//返回给界面的最终的总数据

  var params = {
    pageNo:pageNo,
    pageSize:pageSize,
    keywords:'1'
  }
  //创建MeScroll对象
  var mescroll = initMeScroll("mescroll", {
    down: {
      auto: true, //是否在初始化完毕之后自动执行下拉回调callback; 默认true
      callback: downCallback, //下拉刷新的回调
    },
    up: {
      auto: false, //是否在初始化时以上拉加载的方式自动加载第一页数据; 默认false
      // isBoth: true, //上拉加载时,如果滑动到列表顶部是否可以同时触发下拉刷新;默认false,两者不可同时触发; 这里为了演示改为true,不必等列表加载完毕才可下拉;
      callback: upCallback, //上拉加载的回调
      // onScroll: upCallback, //滚动的回调
      toTop: { //配置回到顶部按钮
        src: "../../../static/images/mescroll/mescroll-totop.png", //默认滚动到1000px显示,可配置offset修改
        offset : 100
      },
      htmlNodata:'<p class="upwarp-nodata"> ————<u>隐</u>哥是有底线的<u>隐</u>————</p>'
    }
  });

  /*下拉刷新的回调 */
  function downCallback() {
    params.pageNo = 1;
    common.ajax({
      url:url,
      method:'get',
      params:params,
      success:function (res) {
        returnTotalLists = res.list;
        if(callback) {callback(returnTotalLists)};
        mescroll.endSuccess(returnTotalLists);
      },
      error:function (res) {
        mescroll.endErr();
      }
    })
  }

  /*上拉加载的回调 */
  function upCallback() {
    if(infiniteLoading) return;
    infiniteLoading = true;
    if(params.pageNo == 1) { params.pageNo = 2 };
    common.ajax({
      url:url,
      method:'get',
      params:params,
      success:function (res) {
        params.pageNo += 1;
        infiniteLoading = false;
        returnTotalLists = returnTotalLists.concat(res.list);
        if(callback) {callback(returnTotalLists)};
        mescroll.endSuccess(res.list);
      },
      error:function (res) {
        mescroll.endErr();
      }
    })
  }

  //禁止PC浏览器拖拽图片,避免与下拉刷新冲突;如果仅在移动端使用,可删除此代码
  document.ondragstart = function() {
    return false;
  }

  /**
   * mescroll 配置
   * 具体参考mescroll官方demo-新浪微博
   */
  function initMeScroll(mescrollId, options) {
    //下拉刷新的布局内容
    var htmlContent = '<div class="downwarp-unload"><p class="downwarp-arrow"></p><p class="downwarp-tip">下拉刷新</p></div>';
    htmlContent += '<div class="downwarp-onload"><p class="downwarp-progress"></p><p class="downwarp-tip">加载中</p></div>';
    //自定义的配置
    var myOption = {
      down: {
        offset: 60, //触发刷新的距离
        htmlContent: htmlContent, //布局内容
        inited: function(mescroll, downwarp) {
          //初始化完毕的回调,可缓存dom
          mescroll.downOnLoadDom = downwarp.getElementsByClassName("downwarp-onload")[0];
          mescroll.downUnloadDom = downwarp.getElementsByClassName("downwarp-unload")[0];
          mescroll.downTipDom = downwarp.getElementsByClassName("downwarp-tip")[0];
          mescroll.downArrowDom = downwarp.getElementsByClassName("downwarp-arrow")[0];
        },
        inOffset: function(mescroll) {
          //进入指定距离范围内那一刻的回调
          mescroll.downOnLoadDom.style.display = "none";
          mescroll.downUnloadDom.style.display = "block";
          mescroll.downTipDom.innerText = "下拉刷新";
          mescroll.downArrowDom.style.webkitTransform = "rotate(0deg)";
          mescroll.downArrowDom.style.transform = "rotate(0deg)";
        },
        outOffset: function(mescroll) {
          //下拉超过指定距离那一刻的回调
          mescroll.downTipDom.innerText = "释放更新";
          mescroll.downArrowDom.style.webkitTransform = "rotate(-180deg)";
          mescroll.downArrowDom.style.transform = "rotate(-180deg)";
        },
        //			onMoving: function(mescroll, rate, downHight) {
        //				//下拉过程中的回调,滑动过程一直在执行; rate下拉区域当前高度与指定距离的比值(inOffset: rate<1; outOffset: rate>=1); downHight当前下拉区域的高度
        //			},
        showLoading: function(mescroll) {
          //触发下拉刷新的回调
          mescroll.downOnLoadDom.style.display = "block";
          mescroll.downUnloadDom.style.display = "none";
          mescroll.downTipDom.innerText = "下拉刷新";
          mescroll.downArrowDom.style.webkitTransform = "rotate(0deg)";
          mescroll.downArrowDom.style.transform = "rotate(0deg)";
        }
      },
      up: {
        toTop: {
          src: "option/mescroll-totop.png" //回到顶部按钮的图片路径
        }
      }
    }
    //加入自定义的默认配置
    options = MeScroll.extend(options, myOption);
    //创建MeScroll对象
    return new MeScroll(mescrollId, options);
  }
}
